<?php

class Vlib_Plugin_Permission extends Zend_Controller_Plugin_Abstract {
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $_request) {
        // Get session permission
        $_objSession = new Zend_Session_Namespace('Permission');
        //if(!empty($_objSession->_memberPermission)) return true;
        $aryPermission = array();
        $currMember = Zend_Auth::getInstance()->getStorage()->read();
        $_objDb = new Vlib_Models_Table();
        // Get info of current member
        if ($currMember) {
            $currMemberId = (int) $currMember->member_id;
            $currMemberDeptId = (int) $currMember->member_dept_id;
        }
        // Get all department
        $_objDb->getListDept($aryDept);
        // Get list group permission
        $_objDb->getListGroupPermission($aryGroup);
        // Get list members in groups permission
        $_objDb->getListMemberGroupPermission($aryMemberGroup);
        // Get list departments in groups permission
        $_objDb->getListDeptGroupPermission($aryDeptGroup);
        // Build tree department and set permission for department
        $aryTreeDept = array();
        $aryTreeDept[0] = array();
        $aryTreeDept[0]['childs'] = array();
        $aryTreeDept[0]['groups'] = array();
        foreach ($aryDept as $index => $dept) {
            $aryTreeDept[$dept['dept_code']] = &$aryDept[$index];
            $aryTreeDept[$dept['dept_code']]['childs'] = array();
            $aryTreeDept[$dept['dept_code']]['groups'] = array();
            foreach($aryDeptGroup as $dg){
                if($dept['dept_code'] == $dg['dept_permission_group_dept_code'])
                    $aryTreeDept[$dept['dept_code']]['groups'][] = $dg['dept_permission_group_permission_group_id'];
            }
        }
        foreach ($aryDept as $index => $dept) {
            $aryTreeDept[(int)$dept['dept_parent_dept_code']]['childs'][] = &$aryDept[$index];
            if(!empty($aryTreeDept[(int)$dept['dept_parent_dept_code']]['groups'])){
                foreach($aryTreeDept[(int)$dept['dept_parent_dept_code']]['groups'] as $group){
                    $aryTreeDept[$dept['dept_code']]['groups'][] = $group;
                }
            }
        }
        foreach($aryGroup as $group){
            // Check member have permission of group
            $havePermission = false;
            foreach($aryMemberGroup as $mg){
                // If current member in list, assign permission for member
                if( $mg['permission_member_group_member_id'] == $currMemberId &&
                    $mg['permission_member_group_permission_group_id'] == $group['permission_group_id'] ){
                        // Assign permission of group for member
                        $aryPermission[] = $group['permission_group_id'];
                        $havePermission = true;
                        break;
                }
            }
            // Check permission with department of current member
            if(!$havePermission){
                if(in_array($group['permission_group_id'], $aryTreeDept[$currMemberDeptId]['groups']))
                        $aryPermission[] = $group['permission_group_id'];
            }
        }
        // Get list permission
        $_aryResult = array();
        /*
         * Struct array permission
         *  array (
         *      [module_name_1] => array(
         *                              [permission_code_11] => [permission_code_11]
         *                              [permission_code_12] => [permission_code_12]
         *                         ),
         *      [module_name_2] => array(
         *                              [permission_code_21] => [permission_code_21]
         *                              [permission_code_22] => [permission_code_22]
         *                         )
         *  )
         */
        if(!empty($aryPermission))
            $_objDb->getPermissionOfMember($_aryResult, $aryPermission);
        // Save to session
        $_objSession->_memberPermission = $_aryResult;
    }
}

?>
